package com.example.antdback.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.antdback.entity.User;
import com.example.antdback.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "业务数据")
@Slf4j
@RestController
@RequestMapping("api")
public class DataController {

    @Resource
    private UserService userService;


    @ApiOperation(value = "用户列表", notes = "测试版本的")
    @GetMapping("users")
    public IPage<User> userList() {
        Page<User> page = userService.page(new Page<>(), new LambdaQueryWrapper<User>().orderByDesc(User::getCreateTime));
        log.info(JSON.toJSONString(page));
        return page;
    }

    @ApiOperation(value = "添加用户", notes = "测试版本的")
    @PostMapping("user/add")
    public String add(@RequestBody User user) {
        long id = System.currentTimeMillis();
        System.out.println(id);
        user.setId(id);
        userService.save(user);
        return "success";
    }

    @PostMapping("user/update")
    public String updateById(@RequestBody User user) {
        userService.updateById(user);
        return "success";
    }

    @PostMapping("user/delete/{id}")
    public String updateById(@PathVariable Integer id) {
        userService.removeById(id);
        return "success";
    }

    @PostMapping("login/account")
    public User login(@Valid @RequestBody User user) {
        User one = userService.lambdaQuery().eq(User::getName, user.getName()).eq(User::getPassword, user.getPassword()).one();
        if(one == null) {
            throw new RuntimeException("用户不存在");
        }
        return one;
    }

    @GetMapping("currentUser")
    public String currentUser() {
        return "{\"name\":\"Serati Ma\",\"avatar\":\"https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png\",\"userid\":\"00000001\",\"email\":\"antdesign@alipay.com\",\"signature\":\"海纳百川，有容乃大\",\"title\":\"交互专家\",\"group\":\"蚂蚁金服－某某某事业群－某某平台部－某某技术部－UED\",\"tags\":[{\"key\":\"0\",\"label\":\"很有想法的\"},{\"key\":\"1\",\"label\":\"专注设计\"},{\"key\":\"2\",\"label\":\"辣~\"},{\"key\":\"3\",\"label\":\"大长腿\"},{\"key\":\"4\",\"label\":\"川妹子\"},{\"key\":\"5\",\"label\":\"海纳百川\"}],\"notifyCount\":12,\"unreadCount\":11,\"country\":\"China\",\"access\":\"admin\",\"geographic\":{\"province\":{\"label\":\"浙江省\",\"key\":\"330000\"}," +
                "\"city\":{\"label\":\"杭州市\",\"key\":\"330100\"}},\"address\":\"西湖区工专路 77 号\",\"phone\":\"0752-268888888\"}";
    }

}
